/**
 * 
 */
package galacticthrone.screen.text;

import galacticthrone.map.namegen.StarNameGen;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.swing.JFrame;

import com.sun.opengl.util.Animator;

/**
 * Application for testing the font renderer.
 *
 * @author Jaco van der Westhuizen
 */
class Test implements WindowListener, GLEventListener
{
	private final Frame window = new JFrame("Testing fonts");
	private final GLCanvas canvas = new GLCanvas();
	private final Animator anim = new Animator(canvas);
	private BitmapFont font;
	private final String[] names = new String[] {StarNameGen.getNewName(), StarNameGen.getNewName(), StarNameGen.getNewName(), StarNameGen.getNewName(), StarNameGen.getNewName()};

	private Test() throws IOException
	{
		window.add(canvas);
		canvas.setPreferredSize(new Dimension(800, 600));
		canvas.addGLEventListener(this);
		window.pack();
		window.addWindowListener(this);
		window.setVisible(true);

		anim.start();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
        {
	        new Test();
        }
        catch (Exception e)
        {
	        e.printStackTrace();
        }
	}

	/* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
     */
    @Override
    public void windowActivated(WindowEvent e)
    {
    }

	/* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
     */
    @Override
    public void windowClosed(WindowEvent e)
    {
	    anim.stop();
	    System.exit(0);
    }

	/* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
     */
    @Override
    public void windowClosing(WindowEvent e)
    {
	    anim.stop();
	    System.exit(0);
    }

	/* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
     */
    @Override
    public void windowDeactivated(WindowEvent e)
    {
    }

	/* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
     */
    @Override
    public void windowDeiconified(WindowEvent e)
    {
    }

	/* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
     */
    @Override
    public void windowIconified(WindowEvent e)
    {
    }

	/* (non-Javadoc)
     * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
     */
    @Override
    public void windowOpened(WindowEvent e)
    {
    }

	/* (non-Javadoc)
     * @see javax.media.opengl.GLEventListener#display(javax.media.opengl.GLAutoDrawable)
     */
    @Override
    public void display(GLAutoDrawable drawable)
    {
	    GL gl = drawable.getGL();

	    // Load font
	    if (font == null)
	    {
	    	gl.glEnable(GL.GL_TEXTURE_2D);
		    gl.glEnable(GL.GL_BLEND);
		    gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);

            font = new BitmapFont(gl, "StarCon Smooth 64.png");
	    }

	    // Display text
	    gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glTranslatef(-1, -1, 0);
		gl.glScalef(2f / 800, 2f / 600, 1);

		font.write(gl, names, Color.WHITE, 64, Font.ALIGN_CENTER, 100, 700, 0, 600);
	    font.write(gl, names, Color.GREEN, 32, Font.ALIGN_CENTER, 100, 400, 0, 250);
	    font.write(gl, names, Color.RED,   16, Font.ALIGN_CENTER, 400, 700, 0, 250);
	    font.write(gl, names, Color.CYAN,   8, Font.ALIGN_CENTER, 400, 700, 0, 125);
	    font.write(gl, names, Color.WHITE,  4, Font.ALIGN_CENTER, 400, 700, 0,  75);

		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
    }

	/* (non-Javadoc)
     * @see javax.media.opengl.GLEventListener#displayChanged(javax.media.opengl.GLAutoDrawable, boolean, boolean)
     */
    @Override
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
    {
    }

	/* (non-Javadoc)
     * @see javax.media.opengl.GLEventListener#init(javax.media.opengl.GLAutoDrawable)
     */
    @Override
    public void init(GLAutoDrawable drawable)
    {
    }

	/* (non-Javadoc)
     * @see javax.media.opengl.GLEventListener#reshape(javax.media.opengl.GLAutoDrawable, int, int, int, int)
     */
    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
    {
    }
}
